package com.peiqi.aiproject.config;

import com.peiqi.aiproject.service.ChatHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
//开启WebSocket服务
@EnableWebSocket
@ConditionalOnProperty(name = "ai.enabled", havingValue = "true")
public class WebSocketConfig  implements WebSocketConfigurer {
    @Bean
    public WebSocketHandler webSocketHandler() {
        return new ChatHandler();
    }
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//        监听chat路径
//        .setAllowedOrigins("*") 解决跨域的问题
        registry.addHandler(webSocketHandler(), "/chat").setAllowedOrigins("*");
    }

}
